<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 优先排钟控制器
 * @package app\index\controller
 */
class RowClock extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取优先排钟列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $page = $this->request->post('page')? '' : 1;
            $limit = $this->request->post('page')? '' : 10;
            $data = Db::name('qt_row_clock')
                ->field('t.*,u.real_name,p.name as project_name')
                ->alias('t')
                ->leftJoin('qt_users u','u.id = t.user_id')
                ->leftJoin('qt_project p','p.id = t.project_id')
                ->where(['t.store_id' => $userInfo['store_id']])
                ->order('t.id desc')
                ->paginate($limit)->each(function ($item){
                    $typeIds = explode(',',$item['work_types']);
                    $workType = Db::name('qt_work_type')->whereIn('id',$typeIds)->column('title','id');
                    $item['work_type'] = $workType;
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加优先排钟
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['project_id']) || empty($getData['no_first']) || empty($getData['work_types'])){
                $this->result('缺少参数', '', 204);
            }
            $projectIds = explode(',',$getData['project_id']);
            $insert = [];
            foreach ($projectIds as $key=>$value){
                $insert[$key] = [
                    'project_id'=>$value,
                    'no_first'=>$getData['no_first'],
                    'work_types'=>$getData['work_types'],
                    'user_id'=>$this->userInfo['id'],
                    'store_id'=>$this->userInfo['store_id'],
                    'created_time'=>time(),
                ];
            }
            $insert = Db::name('qt_row_clock')->insertAll($insert);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑优先排钟
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['project_id']) || empty($getData['no_first']) || empty($getData['work_types'])){
                $this->result('缺少参数', '', 204);
            }
            $update= Db::name('qt_row_clock')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑优先排钟
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_row_clock')->where('id',$getData['id'])->find();
            $this->result('保存成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除优先排钟
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_row_clock')->where('id',$getData['id'])->delete();
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


}
